/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import path from 'path';
import { Worker } from 'worker_threads';

export let cpuUsage = 0;
let worker: Worker | undefined;

/**
 * 开始监测CPU使用率
 */
export function startMonitor(): void {
  if (!worker) {
    worker = new Worker(path.resolve(__dirname, './monitor-script.js'));
    worker.on('message', (data: number) => {
      cpuUsage = data;
    });
  }
}

/**
 * 停止监测CPU使用率
 */
export async function endMonitor() {
  if (worker) {
    await worker.terminate();
  }
}
